/**
 * @Created by pangzg pangzg2@yusys.com.cn on 2020-4-13 13:56:49.
 * @updated by
 * @description 联系人管理
 */
define(function (require, exports) {
	/**
	 * 页面加载完成时触发
	 * @param hashCode 路由ID
	 * @param data 传递数据对象
	 * @param cite 页面站点信息
	 */
	exports.ready = function (hashCode, data, cite) {
		yufp.lookup.reg('USE_ALERTM_MODE');
		yufp.custom.vue({
			el: cite.el,
			data: function () {
				var timeFormat = function (rule, value, callback) {
					if(value){
						var exp=/[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}/;
						if(exp.test(value)){
							callback();
						}else{
							callback(new Error('请输入正确的格式，范例 12:10:10'));
						}
					}else{
						callback();
					}
				};
				
				
				/*定义联系人名称校验*/
				var num = function (rule, value, callback) {
					var reg = /^[0-9]*$/;
					if (reg.test(value)) {
						callback(new Error('联系人名称不得为纯数字'));
					} else {
						callback();
					}
				};
				

				/*定义手机号码校验规则*/
				var phone = function (rule, value, callback) {
					var reg = /^1[3456789]\d{9}$/;
					if (reg.test(value)) {
						callback();
					} else {
						callback(new Error('手机号不合法，请检查'));
					}
				};

				/*定义校验规则，只能填字母数字 下划线*/
				var valiCodes = function (rule, value, callback) {
					var reg = /^[0-9a-zA-Z_]{1,}$/;
					if (value && reg.test(value)) {
						if (value.length > 64) {
							callback(new Error('长度不能大于64位'));
						} else {
							callback();
						}
					} else if (value && !reg.test(value)) {
						callback(new Error('能填字母、数字和下划线'));
					} else {
						callback();
					}
				};

				return {
					appModuleRule:[{required: true,message: '必填项'}],
					appLineRule:[{required: true,message: '必填项'}],
					addDayRule:[{required: true,message: '必填项'},
						{ message: '请输入数字', validator: yufp.validator.number }],
					endTimeRule:[{required: true,message: '必填项'},
						{ validator: timeFormat, trigger: 'blur' }],
					appCodeRule:[{required: true,message: '必填项'}],
					groupCodePatternRule:[{required: true,message: '必填项'}],
					rulesone: [{
						required: true,
						message: '必填项'
					},
					{
						validator: valiCodes,
						trigger: 'blur'
					}
					],
					mobileRule: [
						{
							required: true,
							message: '必填项'
						},
						{
							validator: phone,
							trigger: 'blur'
						}
					],
					nameRule: [
						{
							required: true,
							message: '必填项'
						},
						{
							validator: num,
							trigger: 'blur'
						}
					],

					dataUrl: 'api/citiccard/use/group/taskGroupList',
					relUrl: 'api/citiccard/use/warning/contact/detailsList?',

					addFnButton: true,// !yufp.session.checkCtrl('addFn'),
					updateFnButton: true,//!yufp.session.checkCtrl('modifyFn'),
					deleteFnButton: true,//!yufp.session.checkCtrl('deleteFn'),
					relFnButton: true,//!yufp.session.checkCtrl('relFn'),
					

					formdata: {},
					addRelformdata: {},
					dialogVisible: false,
					formDisabled: false,
					saveDisabled: false,
					cancelBtnShow: true,
					relDialogVisible: false,
					addRelDialogVisible: false,
					viewTitle: "",
					placeholderGroup: '',
					addFlag: true,
					groupDisabled: true,
					searchFormdata: {},
					addSearchFormdata: {},
					options: [],
					oldAppModule:"",
					oldAppLine:"",
					refViewTitle:"",
					oldAppCode:"",
					oldGroupCodePattern:"",
					addRefFlag:true,
				};
			},

			methods: {
				/**
				 * 所属组新增保存
				 */
				saveRelFn: function () {
					var _this = this;
					_this.$refs.addRelForm.validate(function (valid) {
											validate = valid;
										});
					if (!validate) {
						return;
					}
					var model = {};
					yufp.clone(_this.addRelformdata, model);
					var row = _this.$refs.refTable.selections[0];
					if(!_this.addRefFlag){
						model.oldAppCode=_this.oldAppCode;
						model.oldGroupCodePattern=_this.oldGroupCodePattern;
						model.oldAppModule=row.appModule;
						model.oldAppLine=row.appLine;
						url="api/citiccard/use/pattern/ruleUpdate";
					}else{
						url="api/citiccard/use/pattern/ruleAdd";
					}
					model.appModule=row.appModule;
					model.appLine=row.appLine;
					var obj={"appModule":row.appModule,"appLine":row.appLine}
					// 向后台发送保存请求
					yufp.service.request({
						method: 'POST',
						url: url,
						data: model,
						callback: function (code, message, response) {
							if (response.code == 0) {

								// _this.$refs.refRelTable.remoteData();
								var param = {
									condition: JSON.stringify(obj)
								};
								_this.$refs.refRelTable.remoteData(param);
								_this.$message('操作成功');
								_this.addRelDialogVisible = false;
							} else {
								_this.$message({ message: response.message, type: 'error' });
							}
						}
					});
				},
				/**
				 * 所属组删除保存
				 */
				deleteDelFn: function () {
					var _this = this;
					var selections = _this.$refs.refRelTable.selections;
					if (selections.length != 1) {
						_this.$message({ message: '请先选择一条记录', type: 'warning' });
						return;
					}
					var row = _this.$refs.refTable.selections[0];
					var obj={"appModule":row.appModule,"appLine":row.appLine};
					var param="?appCode="+selections[0].appCode+"&groupCodePattern="+selections[0].groupCodePattern+"&appModule="+row.appModule+"&appLine="+row.appLine;
					_this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning',
						center: true,
						callback: function (action) {
							if (action === 'confirm') {
								yufp.service.request({
									method: 'GET',
									url: 'api/citiccard/use/pattern/ruleDel'+param,//删除所属组关联数据
									// data: model,
									callback: function (code, message, response) {
										if (response.code == 0) {
											var param = {
												condition: JSON.stringify(obj)
											};
											_this.$refs.refRelTable.remoteData(param);
											_this.$message('操作成功');
										} else {
											_this.$message({ message: response.message, type: 'error' });
										}
									}
								});
							}
						}
					});
				},
				/**
				 * 所属组新增
				 */
				addRelFn: function () {
					var _this = this;
					_this.addRefFlag=true;
					_this.refViewTitle="新增规则";
					_this.addRelDialogVisible = true;
					_this.$nextTick(function () {
						_this.$refs.addRelForm.resetFields();
					});
				},
				
				refreshTaskGroup: function(){
					var _this=this;
					var datas=_this.$refs.refRelTable.tabledata;
					if(datas.length==0){
						_this.$message('没有需要刷新的');
						return;
					}
					var appCode=datas[0].appCode;
					var url="api/citiccard/use/pattern/refresh/"+appCode;
					// _this.$refs.refRelTable.
					yufp.service.request({
						method: 'GET',
						url: url,
						// data: model,
						callback: function (code, message, response) {
							if (response.code == 0) {
								_this.$message('操作成功');
							} else {
								_this.$message({
									message: response.message,
									type: 'error'
								});
							}
						}
					});
				},
				/**
				 * 所属组新增
				 */
				updateRelFn: function () {
					var _this = this;
					_this.addRefFlag = false;
					_this.refViewTitle = "修改规则";
					if (_this.$refs.refRelTable.selections.length != 1) {
						_this.$message({
							message: '请先选择一条记录',
							type: 'warning'
						});
						return;
					}
					// _this.switchStatus('UPDATE', true);
					_this.addRelDialogVisible=true;
					_this.$nextTick(function () {
						_this.$refs.addRelForm.resetFields();
						var obj = _this.$refs.refRelTable.selections[0];
						_this.oldAppCode=obj.appCode;
						_this.oldGroupCodePattern=obj.groupCodePattern;
						yufp.clone(obj, _this.addRelformdata);
					});
				},
				/**
				 * 所属组查看
				 */
				relFn: function () {
					var _this = this;
					var selectionsAry = _this.$refs.refTable.selections;
					if (selectionsAry.length != 1) {
						_this.$message({
							message: '请先选择一条记录',
							type: 'warning'
						});
						return;
					}
					_this.relDialogVisible = true;
					_this.relUrl = "api/citiccard/use/pattern/ruleList";
					// _this.relUrl = _this.relUrl + "appModule=" + selectionsAry[0].appModule+"&appLine="+selectionsAry[0].appLine;
					this.$nextTick(function () {
						// _this.searchFormdata.rolename = selectionsAry[0].rolename;
						// _this.searchFormdata.rolename = '';
						var obj={"appModule":selectionsAry[0].appModule,"appLine":selectionsAry[0].appLine};
						var param = {
							condition: JSON.stringify(obj),
						};
						_this.$refs.refRelTable.remoteData(param);
						// _this.$refs.refRelTable.remoteData();
					});
				},
				/**
				 * 单击选择
				 */
				cellClick: function (row, event, column, index) {
					var _this = this;
					if (row) {
						_this.$refs.refTable.toggleRowSelection(row);
					}
				},
				/**
				 * 取消新增联系人页面
				 */
				cancelFn: function () {
					var _this = this;
					_this.dialogVisible = false;
				},
				/**
				 * 保存
				 */
				saveFn: function () {
					var _this = this;
					var model = {};
					yufp.clone(_this.formdata, model);
					var validate = false;
					var url = "";
					if (_this.addFlag) {
						url = "api/citiccard/use/group/taskGroupAdd";//新增
					} else {
						url = "api/citiccard/use/group/taskGroupUpdate";//修改
						model.oldAppModule=_this.oldAppModule;
						model.oldAppLine=_this.oldAppLine;
					}
					_this.$refs.refForm.validate(function (valid) {
						validate = valid;
					});
					if (!validate) {
						return;
					}
					// 向后台发送保存请求
					yufp.service.request({
						method: 'POST',
						url: url,
						data: model,
						callback: function (code, message, response) {
							if (response.code == 0) {
								_this.$refs.refTable.remoteData();
								_this.$message('操作成功');
								_this.dialogVisible = false;
							} else {
								_this.$message({
									message: response.message,
									type: 'error'
								});
							}
						}
					});
				},
				/**
				 * 控制保存按钮、xdialog、表单的状态
				 * @param viewType 表单类型
				 * @param editable 可编辑,默认false
				 */
				switchStatus: function (viewType, editable) {
					var _this = this;
					_this.viewType = viewType;
					_this.saveBtnShow = editable;
					_this.dialogVisible = true;
					_this.formDisabled = !editable;
					_this.saveDisabled = !editable;
				},
				/**
				 * 新增按钮
				 */
				addFn: function () {
					var _this = this;
					_this.addFlag = true;
					_this.viewTitle = "应用条线新增";
					_this.switchStatus('ADD', true);
					_this.$nextTick(function () {
						_this.$refs.refForm.resetFields();
					});
				},
				/**
				 * 修改
				 */
				modifyFn: function () {
					var _this = this;
					_this.addFlag = false;
					_this.viewTitle = "应用条线修改";
					if (_this.$refs.refTable.selections.length != 1) {
						_this.$message({
							message: '请先选择一条记录',
							type: 'warning'
						});
						return;
					}
					_this.switchStatus('UPDATE', true);
					_this.$nextTick(function () {
						_this.$refs.refForm.resetFields();
						var obj = _this.$refs.refTable.selections[0];
						_this.oldAppModule=obj.appModule;
						_this.oldAppLine=obj.appLine;
						yufp.clone(obj, _this.formdata);
					});
				},
				/**
				 * 详情
				 */
				infoFn: function () {
					var _this = this;
					_this.addFlag = false;
					_this.viewTitle = "联系人组查看";
					var selectionsAry = _this.$refs.refTable.selections;
					if (selectionsAry.length != 1) {
						_this.$message({
							message: '请先选择一条记录',
							type: 'warning'
						});
						return;
					}
					_this.switchStatus('DETAIL', false);
					_this.$nextTick(function () {
						_this.$refs.refForm.resetFields();
						yufp.clone(selectionsAry[0], _this.formdata);
					});
				},
				/**
				 * 删除
				 */
				deleteFn: function () {
					var _this = this;
					var selections = _this.$refs.refTable.selections;
					if (selections.length < 1) {
						_this.$message({ message: '请先选择一条记录', type: 'warning' });
						return;
					}
					var row=selections[0];
					var appModule=row.appModule;
					var appLine=row.appLine;
					_this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning',
						center: true,
						callback: function (action) {
							if (action === 'confirm') {
								yufp.service.request({
									method: 'GET',
									url: 'api/citiccard/use/group/taskGroupDel?appModule='+appModule+'&appLine='+appLine,//删除
									// data: rolename,
									callback: function (code, message, response) {
										if (response.code == '0') {
											_this.$refs.refTable.remoteData();
											_this.$message('操作成功');
										} else {
											_this.$message({ message: response.message, type: 'error' });
										}
									}
								});
							}
						}
					});
				},
				/**
				 * 翻页保存数据主键
				 */
				getKey: function (row) {
					return row.targetCode + row.alarmMode;
				}
			}
		});
	};
});

